package dao.wx;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.BaseDAO;
import dao.RowMapper;
import entity.Address;

public class MaddressDao extends BaseDAO{
	public void addAddress(int id,String ren,String address,String phone,String yd){
		String sql="insert into tb_address(address_email,address_details,address_name,address_phone,member_id,address_state) values(?,?,?,?,?,?)";
		executeUpdate(sql, yd,address,ren,phone,id,0);	
	}
	public List<Address> addressquery(int id){
		String sql="select * from tb_address  where member_id=? order by address_state desc";
		List<Address> list=executeQuery(sql, new RowMapper<Address>() {
			@Override
			public Address mapper(ResultSet rs) throws SQLException {
				Address one=new Address(rs.getInt(1),
										rs.getString(2),
										rs.getString(3),
										rs.getString(4),
										rs.getString(5),
										rs.getInt(6)==1,
										rs.getInt(7)
										
						);
				return one;
			}
		},id);
		return list;
	}
	public void setDefault(int aid){
		System.out.println(aid);
		String sql1 = "update tb_address set Address_state = 0";
		String sql2 = "update tb_address set Address_state = 1 where address_id = ?";
		super.executeUpdate(sql1);
		super.executeUpdate(sql2, aid);
	}
	public void addressdelete(int id) {
		String sql="delete from tb_address where address_id=?";
		executeUpdate(sql, id);
	}
	public Address addressupdate(int id){
		String sql="select * from tb_address  where address_id=? ";
		Address a=get(sql, new RowMapper<Address>() {
			@Override
			public Address mapper(ResultSet rs) throws SQLException {
				Address one=new Address(rs.getInt(1),
										rs.getString(2),
										rs.getString(3),
										rs.getString(4),
										rs.getString(5),
										rs.getInt(6)==1,
										rs.getInt(7)
						);
				return one;
			}
		},id);
		return a; 
	}
	public void updateaddress(int id, String ren, String address, String phone, String yb) {
		String sql="update tb_address set Address_name = ?,Address_details = ?,Address_phone = ?,Address_email = ? where Address_id = ?";
		executeUpdate(sql, ren,address,phone,yb,id);
		
	}
}
